package com.hmdp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.UUID;
import com.hmdp.constant.RedisConstants;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.User;
import com.hmdp.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@SpringBootTest
public class TokenTest4 {

    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService iUserService;

    @Test
    void method1() {
        List<User> list = iUserService.list();
        for (int i = 0; i < list.size(); i++) {
            User user = list.get(i);
            //保存用户到redis
            String token = UUID.randomUUID().toString(true);
            UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
            Map<String, Object> userMap = BeanUtil.beanToMap(userDTO,
                    new HashMap<>(),
                    CopyOptions.create()
                            .setIgnoreNullValue(true)
                            .setFieldValueEditor((fieldName,fieldValue) -> fieldValue.toString()));
            stringRedisTemplate.opsForHash().putAll(RedisConstants.RedisUserConstants.LOGIN_USER_KEY_PREFIX + token,
                    userMap);
            stringRedisTemplate.expire(RedisConstants.RedisUserConstants.LOGIN_USER_KEY_PREFIX + token,
                    RedisConstants.RedisUserConstants.LOGIN_USER_TTL,
                    TimeUnit.SECONDS);
            System.out.println(token);
        }

    }


}

